package cl.foursoft.sirvicod.dao.facade;

import java.sql.Connection;
import java.util.List;

import cl.foursoft.sirvicod.dao.implementacion.postgresql.PersonaDB;
import cl.foursoft.sirvicod.dao.interfaces.IPersona;
import cl.foursoft.sirvicod.dao.transferObject.PersonaTO;

public class FacadePersona {

	public PersonaTO obtenerPersonaPorRut(String rut, Connection c)
	{
		PersonaTO resp = null;
		
		IPersona personaDB = new PersonaDB(c);		
		resp = personaDB.obtenerPersonaPorRut(rut);
		
		return resp;
	}
	
	public PersonaTO obtenerPersonaFullPorRut(String rut, Connection c)
	{
		PersonaTO resp = null;
		
		IPersona personaDB = new PersonaDB(c);		
		resp = personaDB.obtenerPersonaFullPorRut(rut);
		
		return resp;
	}
	
	public boolean existePersona(String rut, Connection c)
	{
		boolean resp = true;
		
		IPersona personaDB = new PersonaDB(c);
		resp = personaDB.existePersona(rut);
		
		return resp;
	}
	
	public int guardarPersona(PersonaTO persona, Connection c)
	{
		int resp = -1;
		
		IPersona personaDB = new PersonaDB(c);
		resp = personaDB.guardarPersona(persona);
		
		return resp;
	}
	
	public int guardarPersonaFull(PersonaTO persona, Connection c)
	{
		int resp = -1;
		
		IPersona personaDB = new PersonaDB(c);
		resp = personaDB.guardarPersona(persona);
		if(resp > 0)
			resp = personaDB.guardarDatosPersona(persona);
		
		return resp;
	}
	
	public int actualizarPersona(PersonaTO persona, Connection c)
	{
		int resp = -1;
		
		IPersona personaDB = new PersonaDB(c);
		resp = personaDB.actualizarPersona(persona);
		
		return resp;
	}
	
	public int actualizarPersonaFull(PersonaTO persona, Connection c)
	{
		int resp = -1;
		
		IPersona personaDB = new PersonaDB(c);
		resp = personaDB.actualizarPersona(persona);
		if(resp >= 0){
			if(personaDB.existenDatosPersona(persona.getRut())){
				resp = personaDB.actualizarDatosPersona(persona);
			}
			else{
				resp = personaDB.guardarDatosPersona(persona);
			}
		}
		
		return resp;
	}
	
	public List<PersonaTO> obtenerPersonasAutorizadasPorDepto(int depto, Connection c)
	{
		List<PersonaTO> resp = null;
		
		IPersona personaDB = new PersonaDB(c);		
		resp = personaDB.obtenerPersonasAutorizadasPorDepto(depto);
		
		return resp;
	}
	
	public Boolean personaEstaBloqueada(String rut, Connection c)
	{
		Boolean resp = null;
		
		IPersona personaDB = new PersonaDB(c);
		resp = personaDB.personaEstaBloqueada(rut);
		
		return resp;
	}
	
	public int guardarResidente(PersonaTO residente, int numeroDepto, Connection c){
		int resp = -1;
		
		IPersona personaDB = new PersonaDB(c);
		resp = personaDB.guardarResidente(residente, numeroDepto);
		
		return resp;
	}
	
	public int guardarPersonaAutorizada(PersonaTO autorizado, int numeroDepto, Connection c){
		int resp = -1;
		
		IPersona personaDB = new PersonaDB(c);
		resp = personaDB.guardarPersonaAutorizada(autorizado, numeroDepto);
		
		return resp;
	}
	
	public int eliminarResidentes(int numeroDepto, Connection c){
		int resp = -1;
		
		IPersona personaDB = new PersonaDB(c);
		resp = personaDB.eliminarResidentes(numeroDepto);
		
		return resp;
	}
	
	public int eliminarPersonasAutorizadas(int numeroDepto, Connection c){
		int resp = -1;
		
		IPersona personaDB = new PersonaDB(c);
		resp = personaDB.eliminarPersonasAutorizadas(numeroDepto);
		
		return resp;
	}
}
